// 引入组件
import Home from '../pages/Home/Home'

// 一级路由
import Search from '../pages/Search/Search'
import Login from '../pages/Login/Login'
import Register from '../pages/Register/Register'
import Detail from '../pages/Detail/Detail'
import AddCartSuccess from '../pages/AddCartSuccess/AddCartSuccess';
import ShopCart from '../pages/ShopCart/ShopCart';
import Trade from '../pages/Trade/Trade';
import Pay from '../pages/Pay/Pay';
import PaySuccess from '../pages/PaySuccess/PaySuccess';
// 二级路由
import Center from '../pages/Center/Center';
import MyOrder from '../pages/Center/MyOrder/MyOrder';
import GroupOrder from '../pages/Center/GroupOrder/GroupOrder';
// 将路由暴露出去
export default [
  {
    name:'center',
    path:'/center',
    component: () => import('../pages/Center/Center'),
    meta:{show:true},
    children:[
      {
        path:'/center',
        redirect: '/center/myOrder'
      },
      {
        path:'myOrder',
        component: () => import('../pages/Center/MyOrder/MyOrder'),
      },
      {
        path:'groupOrder',
        component: () => import('../pages/Center/GroupOrder/GroupOrder'),
      },
    ]
  },
  {
    caseSensitive:true, // 设置路由区分大小写
    name:'paysuccess',
    path:'/paysuccess',
    component: () => import('../pages/PaySuccess/PaySuccess'),
    meta:{show:true}
    // 此时不用 独享守卫 练习一下 组件内的守卫
  //   beforeEnter:((to, from, next) => {
       // 约束只能在购物车点击结算 跳转到结算页面
  //     if(from.path == '/pay' || from.path == '/'){
  //       next()
  //     }else{
  //       next(false) // next(false)： 回到 from.path 的位置
  //     }      
  //   })
  },
  {
    caseSensitive:true,
    name:'pay',
    path:'/pay',
    component: () => import('../pages/Pay/Pay'),
    meta:{show:true},
    // 约束只能从 trade 页面进入
    beforeEnter:((to, from, next) => {
      // 约束只能在结算页面 跳转到支付页面
      if(from.path == '/trade' || from.path == '/'){
        next()
      }else{
        next(false) // next(false)： 回到 from.path 的位置
      }      
    })
  },
  {
    name:'trade',
    path:'/trade',
    component: () => import('../pages/Trade/Trade'),
    meta:{show:true},
    // 路由独享守卫
    beforeEnter:((to, from, next) => {
      // 约束只能在购物车点击结算 跳转到结算页面
      // console.log(to.path,from.path)
      if(from.path == '/shop'){
        next()
      }else{
        next(false) // next(false)： 回到 from.path 的位置
      }
      // 解决刷新就空白的问题 （因为一刷新时 from.path 会变 /）
      if(from.path == '/'){
        next()
      }      
    })
  },
  {
    name:'shop',
    path:'/shop',
    component: () => import('../pages/ShopCart/ShopCart'),
    meta:{show:true}
  },
  {
    name:'addCartSuccess',
    path:'/addCartSuccess',
    component: () => import('../pages/AddCartSuccess/AddCartSuccess'),
    meta:{show:true}
  },
  {
    name:'detail',
    path:'/detail/:stuId?',
    component: () => import('../pages/Detail/Detail'),
    meta:{show:true}
  },
  {
    name:'home',
    path:'/home',
    component: () => import('../pages/Home/Home'),
    meta:{show:true}
  },
  {
    path:'/',
    redirect:'/home'
  },
  {
    name:'search',
    path:'/search/:keyword?', // 此处的加个 占位符（？） 否则 URL 路径会不准确
    component: () => import('../pages/Search/Search'),
    meta:{show:true}
  },
  {
    name:'login',
    path:'/login',
    component: () => import('../pages/Login/Login'),
    meta:{show:false}
  },
  {
    name:'register',
    path:'/register',
    component: () => import('../pages/Register/Register'),
    meta:{show:false}
  }
]